package br.ufrj.ppgi.greco.controller;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.ufrj.ppgi.greco.model.dao.DAOResource;
import br.ufrj.ppgi.greco.model.dao.DAOType;
import br.ufrj.ppgi.greco.model.entity.EntityResource;

@Resource
public class ResourceController
{
	private DAOType dt;
	private DAOResource dr;
	private Result result;
	
	public ResourceController(DAOResource dr, Result result, DAOType dt)
	{
		this.dr = dr;
		this.dt = dt;
		this.result = result;
	}
	
	@Get
	@Path("/")
	public void indexRedirect()
	{
		this.result.redirectTo(this.getClass()).listAllResources();
	}
	
	
	@Get
	@Path("/config/resources")
	public void listAllResources()
	{
		this.result.include("resources",this.dr.getAll());
	}
	
	@Get
	@Path("/config/resources/new")
	public void newResource()
	{	
		this.result.include("rdfTypes",this.dt.getAllTypesJson());
	}
	
	@Post
	@Path("/config/resources/create")
	public void createResource(EntityResource er)
	{
		this.dr.insert(er);
		this.result.redirectTo(this.getClass()).listAllResources();
	}
	
	
	@Get
	@Path("/config/resources/{id}")
	public void listResource(int id)
	{
		EntityResource er = this.dr.getByID(id);
		
		this.result.include("resource",er);
		this.result.include("staticdps",er.getStaticDataProperties());
		this.result.include("dynamicdps",er.getDynamicDataProperties());
		this.result.include("virtualdps",er.getVirtualDataProperties());
		this.result.include("objectProperties",er.getObjectProperties());
		
		
		this.result.include("rdfTypes",this.dt.getAllTypesJson());
		
	}
	
	@Post
	@Path("/config/resources/update")
	public void updateResource(EntityResource er)
	{
		this.dr.update(er);
		this.result.redirectTo(this.getClass()).listAllResources();
	}
	
	
	@Get
	@Path("/config/resources/{id}/delete")
	public void deleteResource(int id)
	{
		this.dr.deleteByID(id);
		this.result.redirectTo(this.getClass()).listAllResources();
	}
	
	
	
}
